<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AddFriend
 *
 * @author charly
 */
class AddFriend implements Page{
    public function main($args=null) {
        if (Session::getInstance()->sessionCheck() && (isset($args[0]) && !empty($args[0]))) {
            $this->add ($args[0]);
        } else {
            header("Location: /index.php/");
        }
    }

    private function add($id) {
         if (count(Storage::getInstance()->fetch()->query("SELECT ProfilTwo as p FROM Friends WHERE ProfilOne=".$_SESSION["id"]." AND ProfilTwo=".Database::q($id)." UNION SELECT ProfilOne as p FROM Friends WHERE ProfilTwo=".$_SESSION["id"]." AND ProfilOne=".Database::q($id))->fetchAll(PDO::FETCH_ASSOC)) == 0) {
             if (Database::insert("Friends", array($_SESSION['id'], $id))) {
                die("Vous etes desormais ami avec cette personne ! <a href='/index.php/Mur'> Retour </a>");
             } else {
                 die ("Erreur lors de l'ajout..");
             }
         } else {
             die ("Vous etes deja amis avec cette personne !");
         }
    }


    public function pseudoExist($pseudo) {
        return (count(Database::select("Compte", "pseudo", "pseudo=".Storage::getInstance()->fetch()->quote($pseudo) ) ) != 0);
    }
}
?>
